clear all
set more off
pause on
set trace off
cap log close
set matsize 5000
set maxvar 32767
set seed 1234

foreach var in  dummieslab  estout tabout ivregress2 nearstat coefplot {		//Installing programs

	cap which `var'      // check whether -ado- is installed

	qui if _rc ssc install `var', replace // and get it if not
}

			********************************************************************************************
			***REPLICATION CODE FOR AHMED & STASAVAGE "ORIGINS OF EARLY DEMOCRACY", APSR TABLES 1 - 8***
			********************************************************************************************

** Locals

	loc full_controls i.v1858 latitude longitude latitudeXlongitude v1914 v1915 v1916 v1917 v922 v183 
	local y_vars   v1914 rainsd v1915 v1916 v1917  v186 v187 v188 v854 v922 v183 ln_ruggedness v1888 ecodivfao river
	local y_vars_all v1913 `y_vars'


//1. TABLE 1 - Descriptive Statistics by Region

	use analysis, clear		
		
	//. Value Labels
	
		lab def anycouncil 0 "No Council (\%)" ///
						   1 "Council Present (\%)"
						   
		lab val anycouncil anycouncil
		
		lab def central_council 0 "No Council (\%)" ///
								  1 "Council Present (\%)"
						  
		lab val central_council central_council
		
		lab def local_council 0 "No Council (\%)" ///
									 1 "Council Present (\%)"
						  
		lab val  local_council  local_council
					
	* Generating Table 

		qui estpost tab v1858 anycouncil 
		
		#d;
		
			esttab /*using  Table1.tex*/,  replace cell(rowpct(fmt(2)) b(fmt(g) par)) 
			collabels(none) unstack noobs nonumber nomtitle    
			eqlabels(, lhs("\\ \hline \\ \underline{\textbf{Any Council}}")) nolines                     
			varlabels(, blist(Total "\hline \\ Any Council ")) booktabs nonotes title("Descriptive Statistics by Region") nofloat gaps
			;
			
			
		#d cr
		
				
		
//2. TABLE 2 - Descriptive Statistics - Caloric Variability 

	use analysis,clear		
		
	//. Value Labels
	
		lab def anycouncil 0 "No Council" ///
						   1 "Council Present"
						   
		lab val anycouncil anycouncil
		
		lab def central_council 0 "No Council" ///
								  1 "Council Present"
						  
		lab val central_council central_council
		
		lab def local_council 0 "No Council" ///
									 1 "Council Present"
						  
		lab val  local_council  local_council
		
		* Tables

		qui estpost tabstat post1500MaxCal_20km post1500MaxCal_gridcell_20km, by(anycouncil)    statistics(mean sd) columns(statistics) listwise		
		
		#d;
		
			esttab  /* using Table2.tex */,  replace main(mean) aux(sd)  starlevels(* 0.10 ** 0.05 *** 0.01) keep(post1500MaxCal_gridcell_20km)
			collabels(none) unstack noobs nonumber nomtitle label
			eqlabels(, lhs("\hline \\ \underline{\textbf{Any Council}}")) nolines                     
			booktabs nonotes nofloat gaps
			varlabels(post1500MaxCal_gridcell_20km "Caloric Variability"
			post1500MaxCal_20km "Caloric Suitability"
			)
		;
			
			
		#d cr
	
		qui estpost tabstat post1500MaxCal_20km post1500MaxCal_gridcell_20km, by(central_council)    statistics(mean sd) columns(statistics) listwise		
		
		#d;
		
			esttab   /* using Table2.tex */,  append main(mean) aux(sd)  starlevels(* 0.10 ** 0.05 *** 0.01) keep(post1500MaxCal_gridcell_20km)
			collabels(none) unstack noobs nonumber nomtitle label
			eqlabels(, lhs("\hline \\ \underline{\textbf{Central Council}}")) nolines                     
			booktabs nonotes nofloat gaps 
			varlabels(post1500MaxCal_gridcell_20km "Caloric Variability"
					  post1500MaxCal_20km "Caloric Suitability"
			)
			;
			
			
		#d cr
		
		qui estpost tabstat post1500MaxCal_20km post1500MaxCal_gridcell_20km, by(local_council)    statistics(mean sd) columns(statistics) listwise		
		
		#d;
		
			esttab  /* using Table2.tex */,  append main(mean) aux(sd)  starlevels(* 0.10 ** 0.05 *** 0.01) keep(post1500MaxCal_gridcell_20km)
			collabels(none) unstack noobs nonumber nomtitle label
			eqlabels(, lhs("\hline \\ \underline{\textbf{Local Council}}")) nolines                     
			booktabs nonotes nofloat gaps
 			varlabels(post1500MaxCal_gridcell_20km "Caloric Variability"
					  post1500MaxCal_20km "Caloric Suitability"
			)
			;
			
			
		#d cr
						
//3. TABLE 3 - Comparing Societies with High and Low Caloric Variability
			
	use analysis, clear

	
	* Generating 'treatment' indicator
		sum post1500MaxCal_gridcell_20km, d
		gen T=(post1500MaxCal_gridcell_20km>=r(mean))
		
	
	* Generating Balance Table 

		eststo clear

	qui {
		foreach var of local y_vars_all {
		
			//This stores the estimates
			
			eststo E`var'B: xi: reg  `var' T , robust 
			
			//This stores the treatment and control means, #obs and #clusters
			mat mu_C = _b[_cons]
			mat mu_T = _b[_cons] + _b[T]
			mat obs = e(N)
			mat clust = e(N_clust)
			foreach stat in mu_C mu_T obs {
				mat coln `stat' = T
				qui estadd mat `stat' = `stat'
			}
		}
	}	
	
	** Table

		local var "v1913"
		label var T `"`: variable label `var''"'	
		
		esttab E`var'B /* using Table3.tex */, cells("mu_T(fmt(2)) mu_C(fmt(2)) b(fmt(2)star) se(fmt(2)par) obs(fmt(0))") ///
		keep(T) starlevels(* 0.10 ** 0.05 *** 0.01) booktabs  alignment(D{.}{.}{-1}) eqlabels(none) f label nodep nonum noobs nonotes /*nolines*/ nomtitles  gaps replace ///
		collabels("\multicolumn{1}{c}{\specialcell{High Caloric  \\ Variability (0/1) \\ \text{[1]}}}" "\multicolumn{1}{c}{\specialcell{Low Caloric  \\ Variability (0/1) \\ \text{[2]}}}" "\multicolumn{1}{c}{\specialcell{\\ Difference \\ \text{[3]}}}" "\multicolumn{1}{c}{\specialcell{ \\ Std. Err. \\ \text{[4]}}}" "\multicolumn{1}{c}{\specialcell{\\ Obs. \\ \text{[5]}}}") 

		foreach var of local y_vars {
			label var T `"`: variable label `var''"'
			esttab E`var'B /* using Table3.tex */, cells("mu_T(fmt(2)) mu_C(fmt(2)) b(fmt(2)star) se(fmt(2)par) obs(fmt(0))") ///
				keep(T)  starlevels(* 0.10 ** 0.05 *** 0.01)  booktabs  alignment(D{.}{.}{-1}) eqlabels(none) label f nodep nonum noobs nonotes nolines nomtitles  gaps append ///
				collabels(, none)
				 
		}	
		
		
//4. TABLE 4 - Caloric Variability and Council Presence

	use analysis, clear
	
	estimates clear
				
		eststo: xi: reg anycouncil is_post1500MaxCal_gridcell_20km , r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil  if e(sample)==1
		estadd scalar depvarmean=r(mean)
		
		estadd 	 loc FE "No"	
		estadd 	 loc controls "No"		
		estadd	 loc add_controls "No"
		
		eststo: xi: reg anycouncil is_post1500MaxCal_gridcell_20km i.v1858, r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
		
		estadd 	 loc FE "Yes"	
		estadd 	 loc controls "No"
		estadd	 loc add_controls "No"


		eststo: xi: reg anycouncil is_post1500MaxCal_gridcell_20km `full_controls', r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
		
		estadd 	 loc FE "Yes"	
		estadd 	 loc controls "Yes"	
		estadd	 loc add_controls "Yes"
		
		#d;
			esttab /* using Table4.tex */, replace se b(%9.3f) keep(is_post1500MaxCal_gridcell_20km)   eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			mtitles("\specialcell{[1]}" 
					"\specialcell{[2]}" 
					"\specialcell{[3]}"
					"\specialcell{[4]}" 
					"\specialcell{[5]}" 
					)
			mgroups("Any Council (0/1)", pattern(1 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
			varlabels(is_post1500MaxCal_gridcell_20km "Caloric Variability"
					  ln_post1500MaxCal_20km "Caloric Suitability"
			)
			refcat (is_post1500MaxCal_gridcell_20km "\hline", nolabel)
			nonotes  
			stats(FE add_controls r2_a N depvarmean  , layout(@ @ @ @ @)  fmt(a3 a3 a3 a3 a3) labels("Region Fixed Effects" "Controls" "Adj. R-squared" "Observations" "Dependent Variable Mean"))
			;
			
		#d cr
						
//5. TABLE 5 - Councils, Political Integration Hierarchy and Caloric Conditions (OLS Estimates)

	use analysis, clear
		
		estimates clear
		
		eststo : xi: reg  anycouncil  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km, r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil  if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"

		eststo : xi: reg  anycouncil  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km i.v1858 , r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil  if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"
		
		eststo : xi: reg  anycouncil  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km `full_controls' , r
					
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"
	
		eststo : xi: reg  v157  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km, r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum v157 if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"

			
		eststo : xi: reg  v157  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km i.v1858 , r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum v157    if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"

		eststo : xi: reg  v157  is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km `full_controls' , r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum v157  if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"

		#d;
			esttab /* using Table5.tex */, replace se b(%9.3f)  drop(_Iv* latitude longitude latitudeXlongitude v1914 v922 v1915 v1916 v1917 v183  _cons)  
			eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes  starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			mtitles("\specialcell{[1] \\ Any \\ Council}" 
					"\specialcell{[2] \\ Any \\ Council}"
					"\specialcell{[3] \\ Any \\ Council}" 
					"\specialcell{[4] \\ Political \\ Integration}" 
					"\specialcell{[5] \\ Political \\ Integration}"
					"\specialcell{[6] \\ Political \\ Integration}" 
					)
					
			nonotes 
			varlabels(is_post1500MaxCal_gridcell_20km "Caloric Variability"
					  is_post1500MaxCal_20km "Caloric Suitability"
			)
			refcat (is_post1500MaxCal_gridcell_20km "\hline"
					, nolabel)
			stats(FE  add_controls r2_a N  depvarmean, layout(@ @ @ @)  fmt(a3 a3 a3 a3) labels("Region Fixed Effects" "Controls" "Adj. R-squared" "Observations" "Dependent Variable Mean"))

			;
			
		#d cr
			
//6. TABLE 6 - Councils, Political Integration and Caloric Conditions (IV - 2SLS)
				
	use analysis, clear

		estimates clear
	
		eststo : xi: reg anycouncil is_post1500MaxCal_20km is_post1500MaxCal_gridcell_20km `full_controls', r

		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil  if e(sample)==1
		estadd scalar depvarmean=r(mean)
		 
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"	
		estadd	 loc add_controls "Yes"
				
		gen is_mean_cal=is_post1500MaxCal_20km 
		
		gen is_max_var=is_post1500MaxCal_gridcell_20km
		
		lab var is_mean_cal "\emph{sine(\emph{post}-1500 Max. Caloric Suitability -- \emph{20km})}"
				
		eststo : xi: ivregress 2sls anycouncil is_post1500MaxCal_gridcell_20km (v157=is_post1500MaxCal_20km), r first
		
		estat firststage
		mat fstat = r(singleresults)
		
		estadd scalar fs = fstat[1,4] 

		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
		 
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"		
		
		eststo : xi: ivregress 2sls anycouncil is_post1500MaxCal_gridcell_20km (v157=is_post1500MaxCal_20km)  i.v1858, r first
		
		estat firststage
		mat fstat = r(singleresults)
		
		estadd scalar fs = fstat[1,4] 
	

		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil  if e(sample)==1
		estadd scalar depvarmean=r(mean)
		 
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"

		eststo : xi: ivregress 2sls anycouncil is_post1500MaxCal_gridcell_20km (v157=is_post1500MaxCal_20km) `full_controls', r first
		
		estat firststage
		mat fstat = r(singleresults)
		
		estadd scalar fs = fstat[1,4] 
	
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil    if e(sample)==1
		estadd scalar depvarmean=r(mean)
		 
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"		
		estadd	 loc add_controls "Yes"
	
		#d;
			esttab /* using Table6.tex */, replace se b(%9.3f)   drop(_I* _cons latitude longitude latitudeXlongitude v1914 v1915 v1916 v1917 v183 v922) order(is_post1500MaxCal_gridcell_20km is_post1500MaxCal_20km v157) eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes  starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			mtitles("\specialcell{[1] \\ Any \\ Council \\ (OLS) \\ }" 
					"\specialcell{[2] \\ Any \\ Council \\ (IV) \\ }"
					"\specialcell{[3] \\ Any \\ Council \\ (IV) \\ }"
					"\specialcell{[4] \\ Any \\ Council \\ (IV) \\ }"
					"\specialcell{[5] \\ Any \\ Council \\ (IV) \\ }"
					"\specialcell{[6] \\ Any \\ Council \\ (IV) \\ }"
					"\specialcell{[7] \\ Any \\ Council \\ (IV) \\ }"

					)
					
			nonotes 
			varlabels(is_post1500MaxCal_gridcell_20km "Caloric Variability"
					  is_post1500MaxCal_20km "Caloric Suitability"
			)
			refcat (is_post1500MaxCal_gridcell_20km "\hline"
					is_mean_cal "\textbf{First-stage IV}"
			, nolabel)
			stats(FE add_controls fs N depvarmean  , layout(@ @ @ @ @)  fmt(a3 a3 a3 a3 a3) labels("Region Fixed Effects" "Controls"  "\emph{F}-stat first-stage IV"  "Observations" "Dependent Variable Mean"))

			;
			
		#d cr
				
//7. TABLE 7 - Central and Local Council Substitutability

	use analysis, clear
		
		estimates clear
	
		gen  is_post1500MaxCal_comm_20km=is_post1500MaxCal_gridcell_20km
		
		eststo : xi: reg  central_council  c.is_post1500MaxCal_gridcell_20km##i.local_council, r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum central_council  if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"

			
		eststo : xi: reg  central_council  c.is_post1500MaxCal_gridcell_20km##i.local_council i.v1858 , r
			
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum central_council  if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"

		eststo : xi: reg  central_council  c.is_post1500MaxCal_gridcell_20km##i.local_council i.v1858 c.latitude##local_council c.longitude##local_council c.latitudeXlongitude##local_council c.v1914##local_council c.v1915##local_council c.v1916##local_council c.v1917##local_council c.v922##local_council  c.v183##local_council , r
			 
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum central_council   if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"
		
		eststo : xi: reg  local_council  c.is_post1500MaxCal_comm_20km##i.central_council, r
				
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum local_council   if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"

				
		eststo : xi: reg  local_council  c.is_post1500MaxCal_comm_20km##i.central_council i.v1858 , r
				
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum local_council   if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"

		eststo : xi: reg  local_council  c.is_post1500MaxCal_comm_20km##central_council i.v1858 c.latitude##central_council c.longitude##central_council c.latitudeXlongitude##central_council c.v1914##central_council c.v1915##central_council c.v1916##central_council c.v922##central_council c.v1917##central_council c.v183##central_council , r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum local_council   if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"
		
		#d;
			esttab /* using Table7.tex */, replace se b(%9.3f)  keep(1.local_council#c.is_post1500MaxCal_gridcell_20km  1.local_council  is_post1500MaxCal_gridcell_20km 1.central_council#c.is_post1500MaxCal_comm_20km 1.central_council  is_post1500MaxCal_comm_20km)  
			order(1.local_council#c.is_post1500MaxCal_gridcell_20km  1.local_council  is_post1500MaxCal_gridcell_20km 1.central_council#c.is_post1500MaxCal_comm_20km 1.central_council  is_post1500MaxCal_comm_20km
			) 
			eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes  starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			mtitles("\specialcell{[1] \\ Central \\ Council}" 
					"\specialcell{[2] \\ Central \\ Council}"
					"\specialcell{[2] \\ Central \\ Council}"
					"\specialcell{[5] \\ Local \\ Council}" 
					"\specialcell{[6] \\ Local \\ Council}"
					"\specialcell{[6] \\ Local \\ Council}"
					)
					
			nonotes 
			varlabels(1.local_council#c.is_post1500MaxCal_gridcell_20km  "Caloric Variability $\times$ Local Council (0/1)"
					  1.local_council  									"Local Council (0/1)"
					  is_post1500MaxCal_gridcell_20km   						"Caloric Variability"
					  1.central_council#c.is_post1500MaxCal_comm_20km 		"Caloric Variability $\times$ Central Council (0/1)"
					  1.central_council  										"Central Council (0/1)"
					  is_post1500MaxCal_comm_20km								"Caloric Variability"
			)
			refcat (1.local_council#c.is_post1500MaxCal_gridcell_20km "\hline \\ \textbf{Panel A:} \emph{Local Council Present/Absent} \\"
					1.central_council#c.is_post1500MaxCal_comm_20km "\\ \textbf{Panel B:}\emph{ Central Council Present/Absent} \\"
					, nolabel)
			stats(FE add_controls N  depvarmean, layout(@ @ @ @)  fmt(a3 a3 a3 a3) labels("Region Fixed Effects" "Controls" "Observations" "Dependent Variable Mean"))

			;
			
		#d cr
		
//8. TABLE 8 - Council Presence across societies with/without Bureaucracy and Writing

	use analysis, clear
		
		estimates clear
	
		gen  is_post1500MaxCal_writ_20km=is_post1500MaxCal_gridcell_20km
		
		
		eststo : xi: reg  anycouncil  c.is_post1500MaxCal_gridcell_20km##i.bureaucrat, r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"

			
		eststo : xi: reg  anycouncil  c.is_post1500MaxCal_gridcell_20km##i.bureaucrat i.v1858 , r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
			
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"
				
		eststo : xi: reg  anycouncil  c.is_post1500MaxCal_gridcell_20km##i.bureaucrat i.v1858 c.latitude##bureaucrat c.longitude##bureaucrat c.latitudeXlongitude##bureaucrat c.v1914##bureaucrat c.v1915##bureaucrat c.v1916##bureaucrat c.v1917##bureaucrat c.v922##bureaucrat c.v183##bureaucrat   , r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
		
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"
		
		
		#d;
			esttab /* using Table8.tex */, replace se b(%9.3f)  keep(1.bureaucrat#c.is_post1500MaxCal_gridcell_20km  1.bureaucrat  is_post1500MaxCal_gridcell_20km )  
			order(1.bureaucrat#c.is_post1500MaxCal_gridcell_20km  1.bureaucrat  is_post1500MaxCal_gridcell_20km 
			) 
			eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes  starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			mtitles("\specialcell{[1] }" 
					"\specialcell{[2]}"
					"\specialcell{[3] }"
					"\specialcell{[4]}" 
					"\specialcell{[5]}"
					"\specialcell{[6]}"
					)
			mgroups("Any Council (0/1)", pattern(1 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
			nonotes 
			varlabels(1.bureaucrat#c.is_post1500MaxCal_gridcell_20km  "Caloric Variability $\times$ Bureaucrat (0/1)"
					  1.bureaucrat  									"Bureaucrat Present (0/1)"
					  is_post1500MaxCal_gridcell_20km   						"Caloric Variability"
					  1.writing#c.is_post1500MaxCal_writ_20km 		"Caloric Variability $\times$ Writing (0/1)"
					  1.writing  										"Writing Present (0/1)"
					  is_post1500MaxCal_writ_20km								"Caloric Variability"
			)
			refcat (1.bureaucrat#c.is_post1500MaxCal_gridcell_20km "\hline \\ \textbf{Panel A:} \emph{Bureaucrat Present/Absent} \\"
					1.writing#c.is_post1500MaxCal_writ_20km "\\ \textbf{Panel B:}\emph{ Writing Present/Absent} \\"
					, nolabel)
			stats(  N  depvarmean, layout(@ @)  fmt(a3 a3) labels( "Observations" "Dependent Variable Mean"))

			;
			
		#d cr
		
		
		estimates clear
		
		eststo : xi: reg  anycouncil c.is_post1500MaxCal_gridcell_20km##i.writing, r
				
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil   if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "No"	
		estadd 	 loc FE "No"
		estadd	 loc add_controls "No"
		
						
		eststo : xi: reg  anycouncil  c.is_post1500MaxCal_gridcell_20km##i.writing i.v1858 , r		
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil    if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "No"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "No"

		eststo : xi: reg  anycouncil  c.is_post1500MaxCal_gridcell_20km##writing i.v1858 c.latitude##writing c.longitude##writing c.latitudeXlongitude##writing c.v1914##writing c.v1915##writing c.v1916##writing c.v922##writing c.v1917##writing c.v183##writing   , r
		
		sum post1500MaxCal_gridcell_20km   if e(sample)==1
		local meancouncil = r(mean)
		sum anycouncil    if e(sample)==1
		estadd scalar depvarmean=r(mean)
				
		estadd 	 loc controls "Yes"	
		estadd 	 loc FE "Yes"
		estadd	 loc add_controls "Yes"
						
		#d;
			esttab /* using Table8.tex */, append se b(%9.3f)  keep( 1.writing#c.is_post1500MaxCal_gridcell_20km 1.writing  is_post1500MaxCal_gridcell_20km)  
			order(  1.writing#c.is_post1500MaxCal_gridcell_20km 1.writing  is_post1500MaxCal_gridcell_20km
			) nomtitles
			eqlabels(none) f  nodep nobaselevels nolines
			nonum noobs nonotes  starlevels(* 0.10 ** 0.05 *** 0.01) label gaps collabels(, none) nocons 
			nonotes 
			varlabels(1.bureaucrat#c.is_post1500MaxCal_gridcell_20km  "Caloric Variability $\times$ Bureaucrat (0/1)"
					  1.bureaucrat  									"Bureaucrat Present (0/1)"
					  is_post1500MaxCal_gridcell_20km   						"Caloric Variability"
					  1.writing#c.is_post1500MaxCal_gridcell_20km 		"Caloric Variability $\times$ Writing (0/1)"
					  1.writing  										"Writing Present (0/1)"
					  is_post1500MaxCal_gridcell_20km								"Caloric Variability"
			)
			refcat (1.bureaucrat#c.is_post1500MaxCal_gridcell_20km "\hline \\ \textbf{Panel A:} \emph{Bureaucrat Present/Absent} \\"
					1.writing#c.is_post1500MaxCal_gridcell_20km "\\ \textbf{Panel B:}\emph{ Writing Present/Absent} \\"
					, nolabel)
			stats(FE add_controls N  depvarmean, layout(@ @ @ @)  fmt(a3 a3 a3 a3) labels("Region Fixed Effects" "Controls" "Observations" "Dependent Variable Mean"))

			;
			
		#d cr
		
	exit
